﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class ScrollTextAni : MonoBehaviour
    {
        public delegate void OnMsgMoveEnd();
        public OnMoveEnd m_callback;
        public UILabel m_label;

        float speed = 80;
        float showWidth = 550;


        bool is_moving = false;


        float m_begin = 0;
        float m_end = 0;

        public void StartMove(string text)
        {
            m_label.text = text;

            int width = m_label.width;

            m_begin = (showWidth + width) * 0.5f;
            m_end = -m_begin;

            m_label.transform.localPosition = new Vector3(m_begin, 0, 0);

            is_moving = true;
        }

        public void StopMove()
        {
            is_moving = false;
        }

        void Update()
        {
            if (!is_moving)
                return;

            if (m_label.transform.localPosition.x >= m_end)
            {
                float deltaX = -speed * Time.deltaTime;
                m_label.transform.localPosition = new Vector3(m_label.transform.localPosition.x + deltaX, 0, 0);
            }
            else
            {
                is_moving = false;
                OnMoveEnd();
            }
        }

        public void OnMoveEnd()
        {
            if(m_callback != null)
            {
                m_callback();
            }
        }
    }
}

